package com.group.transaction.service.integral.impl;

import com.group.transaction.domain.Integral;
import com.group.transaction.domain.User;
import com.group.transaction.mapper.IntegralMapper;
import com.group.transaction.model.ResultJson;
import com.group.transaction.service.integral.IntegralService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 可用积分业务逻辑层
 *
 * @author YunXing Pang
 * @date 2020/4/27
 */
@Service
public class IntegralServiceImpl implements IntegralService {

    @Resource
    private IntegralMapper integralMapper;

    @Override
    public ResultJson findUserInegral() {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Integral integral = integralMapper.findFirstByUserId(user.getId());
        if (Objects.isNull(integral)) {
            Double points = 0.0;
            integral = Integral.builder().userId(user.getId()).integralTotal(points).integralGive(points).integralUnsed(points).integralUse(points).build();
        }
        return ResultJson.builder().code(1).message("查询成功").result(integral).build();
    }
    /**
     * 通过 用户id 获取 积分
     *
     * @return 查询结果
     */
    @Override
    public ResultJson findUserInegralByUserId(String userId){
        User user = new User();
        user.setId(Long.parseLong(userId));
        Integral integral = integralMapper.findFirstByUserId(user.getId());
        if (integral != null) {
            int points =(int) integral.getIntegralUse() / 30;
            return ResultJson.builder().code(1).message("查询成功").result(points).build();
        }
        return ResultJson.builder().code(-1).message("查询失败").build();
    }
}
